package edu.gyc.test;

import sun.misc.CEFormatException;

import java.util.concurrent.CountDownLatch;

enum CountryEnum{
    ONE(1,"齐"),TWO(2,"楚"),THREE(3,"燕"),
    FOUR(4,"赵"),FIVE(5,"魏"),SIX(6,"韩");
    private Integer code;
    private String  msg;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    CountryEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static CountryEnum foreachCounty(int index) {
        CountryEnum[] countryEnums=CountryEnum.values();
        for (CountryEnum countryEnum : countryEnums) {
            if (index == countryEnum.getCode()) {
                return countryEnum;
            }
        }
        return null;
    }
}

public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 20; i++) {
            makeChina();
        }
    }

    private static void makeChina() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <= 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"国被灭 ");
                countDownLatch.countDown();

            },CountryEnum.foreachCounty(i).getMsg()).start();
        }

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t秦统一六国，秦始皇诞生。");
    }

    private static void closeLight() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 0; i < 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t上晚自习，离开教室。");
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t班长关灯走人。");
    }
}
